package com.zhiyun.yycall.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.gzit.utils.AndroidUtils;
import com.gzit.utils.StringUtils;
import com.zhiyun.common.YYCallUtils;
import com.zhiyun.message.RegistRequest;
import com.zhiyun.message.RegistResponse;
import com.zhiyun.net.AsynCallback;
import com.zhiyun.net.Net;
import com.zhiyun.net.Response;
import com.zhiyun.trace.TraceActivity;
import com.zhiyun.yycall.Config;
import com.zhiyun.yycall.R;

public class RegistActivity extends TraceActivity {

	private EditText phoneNoET;
	private EditText passwordET;
	private EditText referrerET;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.regist_activity);

		phoneNoET = (EditText) this.findViewById(R.id.phoneNo);
		passwordET = (EditText) this.findViewById(R.id.password);
		referrerET = (EditText) this.findViewById(R.id.referrer);

		String phoneNo = AndroidUtils.System.getPhoneNo();
		if (!StringUtils.isBlank(phoneNo)) {
			phoneNoET.setText(phoneNo);
		}

		this.findViewById(R.id.regBtn).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						remoteReg();
					}
				});

	}

	private void remoteReg() {

		final String phoneNo = phoneNoET.getText().toString();
		final String password = passwordET.getText().toString();
		String referrer = referrerET.getText().toString();

		if (StringUtils.isBlank(phoneNo)) {
			AndroidUtils.toastShort("请输入手机号码");
			return;

		}

		if (StringUtils.isBlank(password)) {
			AndroidUtils.toastShort("请输入注册密码");
			return;

		}

		RegistRequest req = new RegistRequest();
		req.setPhoneNo(phoneNo);
		req.setPassword(YYCallUtils.encryptPwd(phoneNo, password));
		req.setReferrer(referrer);

		Net.doHttpRemote(this, req, new RegistResponse(),
				new AsynCallback<Response>() {

					@Override
					public boolean callback(Response r) {
						if (r.isFail()) {
							switch (r.getErrorCode()) {
							case Response.WAITTING_ACTIVE: // 等待激活
								Config.setPhoneNo(phoneNo);
								Config.setPassword(password);
								gotoActiveActivity();
								break;
							case Response.USER_REGISTED:// 用户已经存在
								AndroidUtils.toastShort("账号" + phoneNo
										+ "已经存在。");
								break;
							default:
								AndroidUtils.toastShort("开通账号失败，原因："
										+ r.getExceptionMsg());

								break;

							}

							return true;
						}
						Config.setPhoneNo(phoneNo);
						Config.setPassword(password);
						gotoActiveActivity();
						return true;

					}
				});

	}

	private static final int ACTIVATE_CODE = 1;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case ACTIVATE_CODE:
			if (resultCode == Activity.RESULT_OK) {
				this.setResult(Activity.RESULT_OK);
				this.finish();

			}

			break;
		}

	}

	private void gotoActiveActivity() {
		Intent intent = new Intent();
		intent.setClass(this, ActivateActivity.class);
		intent.putExtra("phoneNo", phoneNoET.getText().toString());
		this.startActivityForResult(intent, ACTIVATE_CODE);
	}

	@Override
	protected void onResume() {
		super.onResume();
		String phoneNo = phoneNoET.getText().toString();
		if (!StringUtils.isBlank(phoneNo)) {
			phoneNoET.setSelection(phoneNo.length());
		}
	}

}
